package com.beaven.imclient.push;

import android.app.IntentService;
import android.content.Context;
import com.beaven.model.model.sp.Account;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import timber.log.Timber;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 */
public class PushIntentService extends GTIntentService {

  public PushIntentService() {
  }

  @Override public void onReceiveServicePid(Context context, int i) {
    Timber.i("onReceiveServicePid---pid-" + i);
  }

  @Override public void onReceiveClientId(Context context, String s) {
    Account.setPushId(s);
    Timber.i("onReceiveClientId---clientId-" + s);
  }

  @Override public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {

    boolean result = PushManager.getInstance()
        .sendFeedbackMessage(context, gtTransmitMessage.getTaskId(),
            gtTransmitMessage.getMessageId(), 90001);
    Timber.d("call sendFeedbackMessage = " + (result ? "success" : "failed"));

    byte[] payload = gtTransmitMessage.getPayload();
    if (payload != null) {
      String msg = new String(payload);
      PushDispatcher.dispatchPush(msg);
      Timber.i("收到的消息--" + msg);
    } else {
      Timber.i("收到的消息--null");
    }
  }

  @Override public void onReceiveOnlineState(Context context, boolean b) {
    Timber.e("onReceiveOnlineState -> " + (b ? "online" : "offline"));
  }

  @Override public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {
    Timber.e("onReceiveCommandResult -> " + gtCmdMessage);
  }
}
